﻿
using Rf.Repository;
using Rf.Support.IServices;
using Rf.Support.Model.Models;

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Rf.Common;
using Rf.IRepository.Base;
using SqlSugar;

namespace Blog.Core.Services
{
    /// <summary>
    /// RoleModulePermissionServices 应用服务
    /// </summary>	
    public class RoleModulePermissionServices : BaseServices<RoleModulePermission>, IRoleModulePermissionServices
    {

        readonly IBaseRepository<Interface> _moduleRepository;
        readonly IBaseRepository<Role> _roleRepository;

        // 将多个仓储接口注入
        public RoleModulePermissionServices(

            IBaseRepository<Interface> moduleRepository,
            IBaseRepository<Role> roleRepository)
        {

            this._moduleRepository = moduleRepository;
            this._roleRepository = roleRepository;
        }

        /// <summary>
        /// 获取全部 角色接口(按钮)关系数据
        /// </summary>
        /// <returns></returns>
        [Caching(AbsoluteExpiration = 10)]
        public async Task<List<RoleModulePermission>> GetRoleModule()
        {
            var roleModulePermissions = await base.Query(a => a.IsDeleted == false);
            var roles = await _roleRepository.Query(a => a.IsDeleted == false);
            var modules = await _moduleRepository.Query(a => a.IsDeleted == false);

            //var roleModulePermissionsAsync = base.Query(a => a.IsDeleted == false);
            //var rolesAsync = _roleRepository.Query(a => a.IsDeleted == false);
            //var modulesAsync = _moduleRepository.Query(a => a.IsDeleted == false);

            //var roleModulePermissions = await roleModulePermissionsAsync;
            //var roles = await rolesAsync;
            //var modules = await modulesAsync;


            if (roleModulePermissions.Count > 0)
            {
                foreach (var item in roleModulePermissions)
                {
                    item.Role = roles.FirstOrDefault(d => d.Id == item.RoleId);
                    item.Interface = modules.FirstOrDefault(d => d.Id == item.ModuleId);
                }

            }
            return roleModulePermissions;
        }



        public async Task<List<RoleModulePermission>> RoleModuleMaps()
        {
            return await QueryMuch<RoleModulePermission, Interface, Role, RoleModulePermission>(
                 (rmp, m, r) => new object[] {
                    JoinType.Left, rmp.ModuleId == m.Id,
                    JoinType.Left,  rmp.RoleId == r.Id
                 },

                 (rmp, m, r) => new RoleModulePermission()
                 {
                     Role = r,
                     Interface = m,
                     IsDeleted = rmp.IsDeleted
                 },

                 (rmp, m, r) => rmp.IsDeleted == false && m.IsDeleted == false && r.IsDeleted == false
                 );
        }

        public async Task<List<RoleModulePermission>> GetRMPMaps()
        {
            return await Db.Queryable<RoleModulePermission>()
              .Mapper(rmp => rmp.Interface, rmp => rmp.ModuleId)
              .Mapper(rmp => rmp.Permission, rmp => rmp.PermissionId)
              .Mapper(rmp => rmp.Role, rmp => rmp.RoleId)
              .Where(d => d.IsDeleted == false)
              .ToListAsync();
        }

        /// <summary>
        /// 批量更新菜单与接口的关系
        /// </summary>
        /// <param name="permissionId">菜单主键</param>
        /// <param name="moduleId">接口主键</param>
        /// <returns></returns>
        public async Task UpdateModuleId(long permissionId, long? moduleId)
        {
            await Db.Updateable<RoleModulePermission>(it => it.ModuleId == moduleId).Where(
                 it => it.PermissionId == permissionId).ExecuteCommandAsync();
        }
    }
}
